#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 09-Nov-09 | LamNPT      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using System.Drawing;
using System.Drawing.Drawing2D;
using Thn.Drawing.Painters;
using Thn.Drawing;
#endregion

namespace Thn.Interface.Vcl.Themes.BlueSea
{
    /// <summary>
    /// Responsible for rendering visual aspects of <see cref="Scroller"/> control.
    /// </summary>
#if DEBUG 
    public
#else
    public
#endif
    class ScrollerRenderer : BlueThemeRendererBase
    {
        #region Fields
        Color mIconEnabledColor = Color.SteelBlue;
        Color mIconDisabledColor = Color.Gray;
        #endregion        

        #region On Render Visual
        /// <summary>
        /// Paint the control's visual stuff (border, background, etc.) onto canvas.
        /// </summary>
        /// <param name="control">The control to be rendered.</param>
        /// <param name="canvas">The canvas on which to paint the control.</param>
        /// <param name="context">The current drawing context.</param>
        protected override void OnRenderVisual(IControl control, Graphics canvas, RenderContext context)
        {
            ScrollerRenderContext rc = (ScrollerRenderContext)context;
            Scroller con = (Scroller)control;
            ITheme theme = con.EffectiveTheme;
            Rectangle r;            

            #region Render track
            PainterBase painter = new White3D();
            r = ConverRectangle(rc.TrackRegion);
            r.Inflate(-1, -1);
            painter.PaintRoundedRectangle(canvas, r, true, true, CornerRadius);
            #endregion

            #region Render Start button
            RenderButton(con, canvas, ConverRectangle(rc.StartRegion), rc.StartButtonState);
            #endregion

            #region Render End button
            RenderButton(con, canvas, ConverRectangle(rc.EndRegion), rc.EndButtonState);
            #endregion

            #region Render Third button
            RenderButton(con, canvas, ConverRectangle(rc.ThirdRegion), rc.ThirdButtonState);
            #endregion

            #region Render Handler button
            RenderHandler(con, canvas, ConverRectangle(rc.HandlerRegion), rc.HandlerState);
            #endregion

            #region Render Icon
            Color c = con.Enabled ? mIconEnabledColor : mIconDisabledColor;
            if (con.LayoutStyle == ScrollerStyle.Horizontal)
            {

                #region Start button arrow
                r = ConverRectangle(rc.StartArrow);
                r.Inflate(-1, -1);
                GraphicUtils.DrawTriangle(canvas, r, 0, c);
                #endregion

                #region Third button arrow
                r = ConverRectangle(rc.ThirdArrow);
                r.Inflate(-1, -1);
                GraphicUtils.DrawTriangle(canvas, r, 0, c);
                #endregion

                #region End button arrow
                r = ConverRectangle(rc.EndArrow);
                r.Inflate(-1, -1);
                GraphicUtils.DrawTriangle(canvas, r, 180, c);
                #endregion
            }
            else
            {
                #region Start button arrow
                r = new Rectangle((int)rc.StartArrow.X, (int)rc.StartArrow.Y, (int)rc.StartArrow.Width, (int)rc.StartArrow.Height);
                r.Inflate(-1, -1);
                GraphicUtils.DrawTriangle(canvas, r, true, c);
                #endregion

                #region Third button arrow
                r = new Rectangle((int)rc.ThirdArrow.X, (int)rc.ThirdArrow.Y, (int)rc.ThirdArrow.Width, (int)rc.ThirdArrow.Height);
                r.Inflate(-1, -1);
                GraphicUtils.DrawTriangle(canvas, r, true, c);
                #endregion

                #region End button arrow
                r = new Rectangle((int)rc.EndArrow.X, (int)rc.EndArrow.Y, (int)rc.EndArrow.Width, (int)rc.EndArrow.Height);
                r.Inflate(-1, -1);
                GraphicUtils.DrawTriangle(canvas, r, false, c);
                #endregion
            }
            #endregion

            #region debug
            //canvas.DrawRectangle(Pens.Red, ConverRectangle(rc.StartRegion));
            //canvas.DrawRectangle(Pens.Beige, ConverRectangle(rc.EndRegion));
            //canvas.DrawRectangle(Pens.Aqua, ConverRectangle(rc.ThirdRegion));
            //canvas.DrawRectangle(Pens.Brown, ConverRectangle(rc.HandlerRegion));
            //canvas.DrawRectangle(Pens.Coral, ConverRectangle(rc.TrackRegion));
            //canvas.DrawRectangle(Pens.Violet, ConverRectangle(rc.StartArrow));
            //canvas.DrawRectangle(Pens.Green, ConverRectangle(rc.EndArrow));
            //canvas.DrawRectangle(Pens.Blue, ConverRectangle(rc.ThirdArrow));
            #endregion
        }
        #endregion

        #region Render Button
        void RenderButton(Scroller con, Graphics canvas, Rectangle region, ButtonState state)
        {
            PainterBase painter = null;
            bool horizontal = con.LayoutStyle == ScrollerStyle.Horizontal;

            #region Choose Gloss
            if (con.Enabled)
            {
                switch (state)
                {
                    case ButtonState.Over:
                        if (horizontal)
                        {
                            painter = new LightBlueGloss();
                        }
                        else
                        {
                            painter = new VerticalScrollLightBlueGloss();
                        }
                        break;
                    case ButtonState.Pressed:
                        if (horizontal)
                        {
                            painter = new BlueGloss();
                        }
                        else
                        {
                            painter = new VerticalScrollBlueGloss();
                        }
                        break;
                    case ButtonState.Normal:
                    default:
                        if (horizontal)
                        {
                            painter = new WhiteGloss();
                        }
                        else
                        {
                            painter = new VerticalScrollWhiteGloss();
                        }
                        break;
                }
            }
            else
            {
                if (horizontal)
                {
                    painter = new GrayGloss();
                }
                else
                {
                    painter = new VerticalScrollGrayGloss();
                }
            }
            #endregion

            region.Inflate(-2, -2);
            if (painter != null && state != ButtonState.Normal)
            {
                painter.PaintRoundedRectangle(canvas, region, true, true, CornerRadius);
            }
            #region Defensive tracing
            else
            {
                // the painter is not initialized.
            }
            #endregion
        }
        #endregion

        #region Render Handler
        void RenderHandler(Scroller con, Graphics canvas, Rectangle region, ButtonState state)
        {
            PainterBase painter = null;
            bool horizontal = con.LayoutStyle == ScrollerStyle.Horizontal;

            #region Choose Gloss
            if (con.Enabled)
            {
                switch (state)
                {
                    case ButtonState.Over:
                        if (horizontal)
                        {
                            painter = new LightBlueGloss();
                        }
                        else
                        {
                            painter = new VerticalScrollLightBlueGloss();
                        }
                        break;
                    case ButtonState.Pressed:
                        if (horizontal)
                        {
                            painter = new BlueGloss();
                        }
                        else
                        {
                            painter = new VerticalScrollBlueGloss();
                        }
                        break;
                    case ButtonState.Normal:
                    default:
                        if (horizontal)
                        {
                            painter = new WhiteGloss();
                        }
                        else
                        {
                            painter = new VerticalScrollWhiteGloss();
                        }
                        break;
                }
            }
            else
            {
                if (horizontal)
                {
                    painter = new GrayGloss();
                }
                else
                {
                    painter = new VerticalScrollGrayGloss();
                }
            }
            #endregion

            region.Inflate(-2, -2);
            if (painter != null)
            {
                painter.PaintRoundedRectangle(canvas, region, true, true, CornerRadius);

                // render icon
                int icW = 2, icH = 8, margin = 2;
                if (horizontal)
                {
                    Rectangle r = new Rectangle(region.X + region.Width / 2, region.Y + region.Height / 4, icW, icH);
                    painter.PaintRectangle(canvas, r, true, true);
                    r = new Rectangle(r.Right + margin, r.Y, r.Width, r.Height);
                    painter.PaintRectangle(canvas, r, true, true);
                    r = new Rectangle(r.Left - (margin * 4), r.Y, r.Width, r.Height);
                    painter.PaintRectangle(canvas, r, true, true);
                }
                else
                {
                    Rectangle r = new Rectangle(region.X + region.Width / 4, region.Y + region.Height / 2, icH, icW);
                    painter.PaintRectangle(canvas, r, true, true);
                    r = new Rectangle(r.X, r.Bottom + margin, r.Width, r.Height);
                    painter.PaintRectangle(canvas, r, true, true);
                    r = new Rectangle(r.X, r.Top - (margin *4), r.Width, r.Height);
                    painter.PaintRectangle(canvas, r, true, true);
                }
            }
            #region Defensive tracing
            else
            {
                // the painter is not initialized.
            }
            #endregion
        }
        #endregion

        #region Conver Rectangle
        Rectangle ConverRectangle(RectangleF rect)
        {
            Rectangle result = new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);

            return result;
        }
        #endregion
        
        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public ScrollerRenderer()
        {
            mDrawVisualCues = false;
        }
        #endregion
    }
}